import axios from 'axios'
import { ElMessage } from 'element-plus'
import config from '@/config'

export async function uploadImage(file: File): Promise<string> {
  const formData = new FormData()
  formData.append('file', file)
  
  try {
    const response = await axios.post(config.uploadApiUrl, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
        Authorization: `Bearer ${localStorage.getItem('token')}`
      }
    })
    
    return response.data.url
  } catch (error: any) {
    ElMessage.error('图片上传失败')
    throw error
  }
}

export function validateImage(file: File) {
  const isImage = /^image\//.test(file.type)
  if (!isImage) {
    ElMessage.error('只能上传图片文件')
    return false
  }
  
  const isLt2M = file.size / 1024 / 1024 < 2
  if (!isLt2M) {
    ElMessage.error('图片大小不能超过 2MB')
    return false
  }
  
  return true
} 